user = {"root": {"passwd": "123", "money": 1000},"admin": {"passwd": "admin", "money": 1000}}
product = {'f001': {"name": "apple", "price": 2.5},
                   'f002': {"name": "banana", "price": 3.5},
                   'f003': {"name": "grape", "price": 4.5},
                   'f004':{"name":"fork","price":18},
                   'f005':{"name":"sheep","price":25}}
print("-" * 50)
print('#' + "{0:^42}".format("欢迎登录三乐购物系统") + '#')
print("-" * 50)
print("温馨提示，输入quit退出系统")


def product_list():
    print("商品列表")
    for i, j in product.items():
        print(f"编号：{i}，名称：{j['name']},单价：{j['price']}")


def menu():
    print("菜单栏".center(40, "-"))
    print("输入1查看商品列表".center(43))
    print("输入2查看购物车 ".center(43))
    print("输入3进行购买   ".center(43))
    print("输入4结算     ".center(43))
    print("输入5退出系统   ".center(43))
    print("-" * 43)


def cart():
    global sum_price
    print("购物车".center(40, "*"))
    print("已购买的商品：")
    for i in buy_pro:
        print(f"名称：{i[0]} 数量：{i[1]}")
    print(f"总金额为：{sum_price}")
    if len(buy_pro)==0:
        print("暂未购买物品，购物车为空")
    print("购物车".center(40, "*"))

def buy():
    global sum_price,have_money
    print("商品列表")
    for i, j in product.items():
        print(f"编号：{i}，名称：{j['name']},单价：{j['price']}")
    print("输入quit退出购买")
    while 1:
        buy = input("请输入要购买的物品编号:")
        if buy == 'quit':
            break
        if buy not in product.keys():
            print("输入有误，请重新输入")
            continue

        Input = input("请输入要购买的数量:")
        if Input.isdigit():
            count = float(Input)
        else:
            print("输入有误，请重新输入")
            continue

        have_money = have_money - product[buy]['price'] * count
        if have_money < 0:
            have_money = have_money + product[buy]['price'] * count
            print(f"所需价格：{product[buy]['price'] * count}，余额为：{have_money}")
            print("余额不足不可以购买")
        else:
            print(f"你的余额为{have_money}")
            sum_price = product[buy]["price"] * count + sum_price
            buy_pro.append([product[buy]['name'], count])


def calculate():
    button = input("购买结束，是否选择支付，输入yes付款，no退出：")
    if button == "no":
        return 0
    else:
        user[username]["money"] -= sum_price
        print("支付成功！")
        print(f"余额：{user[username]['money']}")


while 1:
    username = input("请输入用户名：")
    if username == 'quit':
        break
    pwd = input("请输入密码：")
    if username not in user.keys():
        print("用户名不存在")
        continue

    elif user[username]['passwd'] == pwd:
        print("登录成功!")
        have_money = user[username]['money']
        print("-" * 50)
        buy_pro = []
        sum_price = 0.0
        menu()
        while 1:

            b = input("请选择你要进行的操作：")
            if b == '1':
                product_list()
            elif b == '2':
                cart()
            elif b == '3':
                buy()
            elif b == '4':
                calculate()
            elif b == '5':
                print("成功退出！")
                break
        break
    else:
        print("密码错误")
        continue




